<?php
class CmsconfigurationsController extends AppController {
    var $name = 'Cmsconfigurations';
    var $helpers = array('Html','Form','Accessibility','Pagination', 'Fck');
    var $uses = array('Organization', 'Cmsconfiguration','Web');
    
    function index() {
       $this->redirect('/cmsconfigurations/edit/');
    }
    
    function edit() {
        $title = 'Editar Página Principal';
        $this->set('title', $title);
        $this->set('pageTitle', $title);
        
        $organization=$this->othAuth->getData('organization');
        $organization = $this->Organization->find(array('Organization.id' => $organization['id']));
        
        $web = $this->Web->find(array('Organization.id' => $organization['Organization']['id']));
        $this->set('web', $web['Web']);
        
        $id=$organization['Organization']['cmsconfiguration_id']; 
        $this->Cmsconfiguration->id = $id;
        if (empty($this->data)) {
			$this->data = $this->Cmsconfiguration->read();
		} else {
			
			$web = $this->Organization->Web->find(array('Organization.id' => $organization['Organization']['id']));
			$cmsconfiguration = $this->Organization->Cmsconfiguration->find(array('Organization.id' => $organization['Organization']['id']));
			    
			if (isset($web['Web']['id'])) {
				$this->data['Web']['id'] = $web['Web']['id'];
			}
			if (isset($cmsconfiguration['Cmsconfiguration']['id']) && (!empty($cmsconfiguration['Cmsconfiguration']['id']))) {
				$this->data['Cmsconfiguration']['id'] = $cmsconfiguration['Cmsconfiguration']['id'];
			}
			
			if ($this->Web->save($this->data)) {
				if (!isset($this->data['Web']['id']) || (empty($this->data['Web']['id']))) {
					$web_id = $this->Web->getLastInsertId();
					$this->data['Organization']['id'] = $organization['Organization']['id'];
					$this->data['Organization']['web_id'] = $web_id;
					
					if ($this->Organization->save($this->data)) {
						$_SESSION['mensagem']['success'][] = 'Saudação cadastrada com sucesso!';
					} else {
						$_SESSION['mensagem']['error'][] = 'Erro ao tentar cadastrar Saudação.';
					}
				}
			} else {
				$_SESSION['mensagem']['error'][] = 'Erro ao tentar cadastrar CMS.';
			}
			        
			if($this->Cmsconfiguration->save($this->data)){

				if(!isset($this->data['Cmsconfiguration']['id']) || empty($this->data['Cmsconfiguration']['id'])){
					$cmsconfiguration_id = $this->Cmsconfiguration->getLastInsertId();   
					$this->data['Organization']['id'] = $organization['Organization']['id'];
					$this->data['Organization']['cmsconfiguration_id'] = $cmsconfiguration_id;
					if ($this->Organization->save($this->data)) {
						$_SESSION['mensagem']['success'][] = 'Página principal salvo com sucesso.';
					} else {
						$_SESSION['mensagem']['error'][] = 'Erro ao tentar cadastrar Permissões de Visualização.';
					}
				}
			} else {
				$_SESSION['mensagem']['error'][] = 'Erro ao tentar cadastrar CMS.';
			}
			$this->redirect('/cmsconfigurations/edit/');
		}
        
        $cmsconfiguration = $this->Cmsconfiguration->find(array('Cmsconfiguration.id' => $id));
        $this->set('cmsconfiguration', $cmsconfiguration );
    }
}

